package com.steven.ourcontest.chat.view

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.gyf.immersionbar.ImmersionBar
import com.steven.ourcontest.R
import com.steven.ourcontest.base.BaseActivity
import com.steven.ourcontest.base.BaseFragment
import com.steven.ourcontest.chat.widget.ChatTabBar.Companion.TAB_FRIENDS
import com.steven.ourcontest.chat.widget.ChatTabBar.Companion.TAB_MESSAGE
import com.steven.ourcontest.chat.widget.OnSelectListener
import kotlinx.android.synthetic.main.activity_friends.*
import kotlinx.android.synthetic.main.view_chat_tab.view.*

class FriendsActivity : BaseActivity() {
    private val mAdapter by lazy {
        AdapterFragmentPager(this)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initListener()
    }

    override fun getViewId(): Int? {
        return R.layout.activity_friends
    }

    override fun initView() {
        pager_chat?.adapter = mAdapter
        pager_chat?.isUserInputEnabled = true
        pager_chat?.offscreenPageLimit = 2
        pager_chat?.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                when (position) {
                    0 -> {
                        chat_bar?.slideItem(TAB_MESSAGE)
                        ImmersionBar.with(this@FriendsActivity)
                            .statusBarColor(R.color.app_base)
                            .autoDarkModeEnable(true)
                            .init()
                    }
                    1 -> {
                        chat_bar?.slideItem(TAB_FRIENDS)
                        ImmersionBar.with(this@FriendsActivity)
                            .statusBarColor(R.color.app_base)
                            .autoDarkModeEnable(true)
                            .init()
                    }
                }
            }
        })
        chat_bar?.selectListener = object : OnSelectListener {
            override fun onSelected(current: Int, index: Int): Boolean {
                pager_chat?.setCurrentItem(index, false)
                return true
            }
        }
    }

    private fun initListener() {

    }
}

class AdapterFragmentPager(fragmentActivity: FragmentActivity) :
    FragmentStateAdapter(fragmentActivity) {

    private val fragments: ArrayList<BaseFragment> = ArrayList()

    init {
        fragments.add(MessageFragment())
        fragments.add(FriendsFragment())
    }

    override fun createFragment(position: Int): Fragment {
        return fragments[position]
    }

    override fun getItemCount(): Int {
        return fragments.size
    }
}